package kr.hkit.powerjava.chapter25.chat;

public class QuizProtocol {
	private static final int WAITING = 0;
	private static final int PROBLEM = 1;
	private static final int ANSWER = 2;
	
	private static final int NUMPROBLEMS = 3;	//문제수
	
	private int state = QuizProtocol.WAITING;	//현재상태
	private int currentProblem = 0;	//현재 문제
	
	private String[] problem = {"네트워크 처리 패키지는?", 
								"자바의 최신버전은?",
								"인터넷에서 컴퓨터를 식별하는 주소는?"};
	private String[] answers = {"java.io", 
								"1.7",
								"IP주소"};
	
	public String process(String theInput) {
		String theOutput = null;
		switch(state) {
			case QuizProtocol.WAITING:
				theOutput = "퀴즈를 시작합니다.(y/n)";
				state = QuizProtocol.PROBLEM;
				break;
			case QuizProtocol.PROBLEM :
				if(theInput.equalsIgnoreCase("y")) {	//대소문자구분안함
					theOutput = problem[currentProblem];	//0번문제 출제
					state = QuizProtocol.ANSWER;
				}else {
					state = QuizProtocol.WAITING;
					theOutput = "quit";
				}
				break;
			case QuizProtocol.ANSWER :
				if(theInput.equalsIgnoreCase(answers[currentProblem])) {
					theOutput = "정답입니다. 계속하시겠습니까?(y/n)";
					state = QuizProtocol.PROBLEM;
				} else {
					theOutput = "오답입니다. 계속하시겠습니까?(y/n)";
					state = QuizProtocol.PROBLEM;
				}
				currentProblem = (currentProblem + 1) % NUMPROBLEMS;
				//문제 1씩 증가
				//문제 수와 나눔. 0, 1, 2 됨
				break;
		}
		return theOutput;
	}
}